import WidgetBaseConfig from './WidgetBaseConfig'
import shortid from 'shortid'
import WidgetRuntimeConfig from './WidgetRuntimeConfig';
import WidgetDefinition from './WidgetDefinition';

export default class WidgetConfig {
  /** 组件名称 */
  public name = '组件';

  /** 组件ID */
  public id = shortid.generate()

  /** lock */
  public lock = false

  /** show */
  public visible = true

  /** 组件基本设置 */
  public base: WidgetBaseConfig = new WidgetBaseConfig()

  /** 组件的父组件 */
  public parentWidget: WidgetConfig | null = null;

  /** 组件的子组件 */
  public children: WidgetConfig[] = [];

  /** 组件的TAG名称 */
  public _widgetTag = 'DefaultWidget';

  /** 组件的代码片段 */
  public _code = ''

  /** 运行时状态 */
  public _runtime: WidgetRuntimeConfig = new WidgetRuntimeConfig()

  public widgetDefinition: WidgetDefinition | undefined = undefined

  public getAbsolutePosition () {
    let x = this.base.x
    let y = this.base.y

    if (this.parentWidget) {
      const { x: dx, y: dy} = this.parentWidget.getAbsolutePosition()
      x += dx
      y += dy
    }

    return {x, y}
  }

  public constructor(widgetDefinition?: WidgetDefinition) {
    if (widgetDefinition) {
      this.widgetDefinition = widgetDefinition
      this._widgetTag = widgetDefinition.tag
      this.name = widgetDefinition.displayName
      if (widgetDefinition.autoSize) {
        this.base.w = 0
        this.base.h = 0
      }
    }
    widgetDefinition?.propsGroups.forEach((propsGroup) => {
      if (propsGroup.prop) {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        (this as any) [propsGroup.prop] = propsGroup.defaultValue || false
      }
      propsGroup.propsSchemas.forEach((d) => {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        (this as any) [d.prop] = d.value
      })

    })
  }
}
